package com.prestamomo.vil.ppa.pmvilpage;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilSourceFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PmvilSourceVy extends PmvilAtyRoot {

    private ImageView sourcePmvilIvback;
    private EditText sourcePmvilEtemail;
    private EditText sourcePmvilEtfacebook;
    private LinearLayout sourcePmvilLljy;
    private TextView sourcePmvilTvjy;
    private LinearLayout sourcePmvilLlyt;
    private TextView sourcePmvilTvyt;
    private LinearLayout sourcePmvilLlgz;
    private TextView sourcePmvilTvgz;
    private LinearLayout sourcePmvilLlsr;
    private TextView sourcePmvilTvsr;
    private TextView sourcePmvilTvtj;

    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilJyList = new ArrayList<>();
//    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilHyList = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilGzList = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilSrList = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilGxList = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> sourcePmvilUseList = new ArrayList<>();
    private String pmvilJycode;
//    private String pmvilHycode;
    private String pmvilGzcode;
    private String pmvilSrcode;
    private String pmvilUsecode;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.source_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_mghrthkyf);
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_mghrthkyf_0);
        sourcePmvilIvback = findViewById(R.id.source_pmvil_iv_back);
        sourcePmvilEtemail = findViewById(R.id.source_pmvil_et_email);
        sourcePmvilEtfacebook = findViewById(R.id.source_pmvil_et_facebook);
        sourcePmvilLljy = findViewById(R.id.source_pmvil_ll_jy);
        sourcePmvilTvjy = findViewById(R.id.source_pmvil_tv_jy);
        sourcePmvilLlyt = findViewById(R.id.source_pmvil_ll_yt);
        sourcePmvilTvyt = findViewById(R.id.source_pmvil_tv_yt);
        sourcePmvilLlgz = findViewById(R.id.source_pmvil_ll_gz);
        sourcePmvilTvgz = findViewById(R.id.source_pmvil_tv_gz);
        sourcePmvilLlsr = findViewById(R.id.source_pmvil_ll_sr);
        sourcePmvilTvsr = findViewById(R.id.source_pmvil_tv_sr);
        sourcePmvilTvtj = findViewById(R.id.source_pmvil_tv_tj);

    }

    @Override
    protected void clickePmvil() {
        sourcePmvilIvback.setOnClickListener(v -> showPmvilWaliuDlg());
        sourcePmvilLljy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSourcePmvilDgl(sourcePmvilLljy, sourcePmvilTvjy, "教育", sourcePmvilJyList);
            }
        }); sourcePmvilLlyt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSourcePmvilDgl(sourcePmvilLlyt, sourcePmvilTvyt, "用途", sourcePmvilUseList);
            }
        });

        sourcePmvilLlgz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSourcePmvilDgl(sourcePmvilLlgz, sourcePmvilTvgz, "工作類型", sourcePmvilGzList);
            }
        });
        sourcePmvilLlsr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sourcePmvilSrList.size() != 0) {
                }
                showSourcePmvilDgl(sourcePmvilLlsr, sourcePmvilTvsr, "月收入", sourcePmvilSrList);
            }
        });

        sourcePmvilTvtj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(sourcePmvilEtemail.getText().toString())||TextUtils.isEmpty(sourcePmvilEtfacebook.getText().toString())
                        ||TextUtils.isEmpty(sourcePmvilTvjy.getText().toString())
                        ||TextUtils.isEmpty(sourcePmvilTvgz.getText().toString())||TextUtils.isEmpty(sourcePmvilTvsr.getText().toString())){
                    PmvilTosMd.showPmvaMsg("資訊不完整");
                } else if (!pmvilIsEmail(sourcePmvilEtemail.getText().toString())) {
                    PmvilTosMd.showPmvaMsg("電子郵件格式不正確。");
                } else {
                    Intent pmvilSource2Int = new Intent(pmvilAy, PmvilSource2Vy.class);
                    pmvilSource2Int.putExtra("pmvil_email",sourcePmvilEtemail.getText().toString());
                    pmvilSource2Int.putExtra("pmvil_facebook",sourcePmvilEtfacebook.getText().toString());
                    pmvilSource2Int.putExtra("pmvil_jycode",pmvilJycode);
                    pmvilSource2Int.putExtra("pmvil_ytcode",pmvilUsecode);
                    pmvilSource2Int.putExtra("pmvil_gzcode",pmvilGzcode);
                    pmvilSource2Int.putExtra("pmvil_srcode",pmvilSrcode);
                    pmvilSource2Int.putExtra("pmvil_gxList", (Serializable) sourcePmvilGxList);
                    startActivity(pmvilSource2Int);
                    finish();
                }
            }
        });
    }

    private void showSourcePmvilDgl(LinearLayout sourcePmvilLl, TextView sourcePmvilTv, String sourcePmvilStr, List<PmvilSerConfig.ExtInfoArrPmvil> pmvilSourc) {
        View soucrPmvilView = LayoutInflater.from(this).inflate(R.layout.source_pmvil_pop_ly, null, false);
        final PopupWindow soucrPmvilPop = new PopupWindow(soucrPmvilView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        soucrPmvilPop.setTouchable(true);
        soucrPmvilPop.showAsDropDown(sourcePmvilLl);
        ImageView soucrPmvilPopIvback = soucrPmvilView.findViewById(R.id.source_pmvil_pop_iv_back);
        TextView soucrPmvilPopTvtitle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_tv_title);
        RecyclerView soucrPmvilPopRecycle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_recycle);
        soucrPmvilPopTvtitle.setText(sourcePmvilStr);
        soucrPmvilPopIvback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soucrPmvilPop.dismiss();
            }
        });
        PmvilSourceFiter pmvilSourceFiter = new PmvilSourceFiter(pmvilAy, pmvilSourc);

        soucrPmvilPopRecycle.setAdapter(pmvilSourceFiter);
        pmvilSourceFiter.setSourceFilerIngter(new PmvilSourceFiter.SourceFilerIngter() {
            @Override
            public void setPmvilSourCheckBean(PmvilSerConfig.ExtInfoArrPmvil pmvilSourPopBen) {
                sourcePmvilTv.setText(pmvilSourPopBen.getPmvilname());
                if (sourcePmvilStr.equals("教育")) {
                    pmvilJycode = pmvilSourPopBen.getPmvilcode();
                } else if (sourcePmvilStr.equals("用途")) {
                    pmvilUsecode = pmvilSourPopBen.getPmvilcode();
                }else if (sourcePmvilStr.equals("工作類型")) {
                    pmvilGzcode = pmvilSourPopBen.getPmvilcode();
                } else if (sourcePmvilStr.equals("月收入")) {
                    pmvilSrcode = pmvilSourPopBen.getPmvilcode();
                }
                soucrPmvilPop.dismiss();
            }
        });
    }

    @Override
    protected void loadPmvilData() {
        showPmvilLoading();
        PmvilHttpRequest.getConfigser(this, "code", new PmvilCallBack<List<PmvilSerConfig>>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, List<PmvilSerConfig> pmvilContent) {
                hindPmvilLoading();
                for (PmvilSerConfig pmvilSerConfig : pmvilContent) {
                    if (pmvilSerConfig.getPmvilconfigKey().equals("education")) {
                        sourcePmvilJyList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    } /*else if (pmvilSerConfig.getPmvilconfigKey().equals("marital")) {
                        sourcePmvilHyList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    }*/ else if (pmvilSerConfig.getPmvilconfigKey().equals("workType")) {
                        sourcePmvilGzList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    } else if (pmvilSerConfig.getPmvilconfigKey().equals("monthIncome")) {
                        sourcePmvilSrList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    } else if (pmvilSerConfig.getPmvilconfigKey().equals("socialIdentity")) {
                        sourcePmvilUseList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    } else if (pmvilSerConfig.getPmvilconfigKey().equals("relationship")) {
                        sourcePmvilGxList.addAll(pmvilSerConfig.getPmvilextInfoArr());
                    }
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, List<PmvilSerConfig> pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, List<PmvilSerConfig> pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    public static boolean pmvilIsEmail(String pmvilEmailStr){
        return TextUtils.isEmpty(pmvilEmailStr)?false:pmvilEmailZew(pmvilEmailStr);
    }

    private static boolean pmvilEmailZew(String pmvilEmailStr) {
        Pattern compileC = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        Matcher matcherM = compileC.matcher(pmvilEmailStr);
        return matcherM.matches();
    }


    @Override
    public void onBackPressed() {
        showPmvilWaliuDlg();
    }
}
